package com.liveinstars.im.domain.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liveinstars.im.domain.dao.mapper.FansMapper;
import com.liveinstars.im.domain.dao.model.Fans;
import com.liveinstars.im.domain.manager.FansManager;
import com.onepiece.shipelves.common.enums.SystemExceptionEnum;
import com.onepiece.shipelves.common.exception.DatabaseException;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 粉丝表 服务实现类
 * </p>
 *
 * @author 8btc
 * @since 2020-03-31
 */
@Service
public class FansManagerImpl extends ServiceImpl<FansMapper, Fans> implements FansManager {

    private final static Logger logger = LoggerFactory.getLogger(FansManagerImpl.class);

    @Override
    public List<Fans> listByUserId(Long userId) {
        try {
            QueryWrapper<Fans> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userId);
            List<Fans> list = this.list(queryWrapper);
            return list;
        } catch (Exception e) {
            logger.error("listByUserId userId:{} errorMsg:{}", userId, e.getMessage(), e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }
    }

    @Override
    public List<Fans> listByFansId(Long fansId) {
        try {
            QueryWrapper<Fans> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("fans_id", fansId);
            List<Fans> list = this.list(queryWrapper);
            return list;
        } catch (Exception e) {
            logger.error("listByFansId fansId:{} errorMsg:{}", fansId, e.getMessage(), e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }
    }

    @Override
    public void add(Fans fans) {
        try {
            this.save(fans);
        } catch (Exception e) {
            logger.error("add fans:{} errorMsg:{}", fans.toString(), e.getMessage(), e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }
    }

    @Override
    public void deleteByUserIdAndFansId(Long userId, Long fansId) {
        try {
            QueryWrapper<Fans> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userId).eq("fans_id", fansId);
            this.remove(queryWrapper);
        } catch (Exception e) {
            logger.error("deleteByUserIdAndFansId user_id:{} fans_id:{} errorMsg:{}", userId, fansId, e.getMessage(), e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }
    }

    @Override
    public Fans getByUserIdAndFansId(Long userId, Long fansId) {
        try {
            QueryWrapper<Fans> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userId).eq("fans_id", fansId);
            List<Fans> list = this.list(queryWrapper);
            if (CollectionUtils.isNotEmpty(list)) {
                return list.get(0);
            } else {
                return null;
            }
        } catch (Exception e) {
            logger.error("getByUserIdAndFansId user_id:{} fans_id:{} errorMsg:{}", userId, fansId, e.getMessage(), e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }
    }

    @Override
    public Integer countByFansId(Long fansId) {
        try {
            QueryWrapper<Fans> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("fans_id", fansId);
            int count = this.count(queryWrapper);
            return count;
        } catch (Exception e) {
            logger.error("countByFansId fans_id:{} errorMsg:{}", fansId, e.getMessage(), e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }
    }

    @Override
    public Integer countByUserId(Long userId) {
        try {
            QueryWrapper<Fans> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userId);
            int count = this.count(queryWrapper);
            return count;
        } catch (Exception e) {
            logger.error("countByUserId user_id:{} errorMsg:{}", userId, e.getMessage(), e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }
    }
}
